import {apiJscode2session} from './api'

export function shuffleArray(array) {
	for (let i = array.length - 1; i > 0; i--) {
		const j = Math.floor(Math.random() * (i + 1));
		var temp = array[i]
		array[i] = array[j]
		array[j] = temp
		//  [array[i], array[j]] = [array[j], array[i]];
	}
	return array;
}
/**
 * 是否授权
 */
export function getUserSetting() {
	const user = {}
	return new Promise((resolve, reject) => {
		const userSignature = wx.getStorageSync('userSignature')
		if( userSignature !=='' ) {
			resolve(userSignature)
			return
		}
		wx.getSetting({
			success(res) {
				if (res.authSetting['scope.userInfo']) {
					console.log('已授权');
					// 已经授权，可以直接调用 getUserInfo 获取头像昵称
					wx.getUserInfo({
						success: function (userInfoRes) {
							wx.setStorageSync('userSignature', userInfoRes.signature)
							// user.nickName = userInfoRes.userInfo.nickName
							// user.avatarUrl = userInfoRes.userInfo.avatarUrl
							// user.province = userInfoRes.userInfo.province
							// user.city = userInfoRes.userInfo.city
							resolve(userInfoRes.signature)
						}
					})
				}
			}
		})
	})
}

export async function wxlogin() {
	return new Promise(async (resolve, reject) => {
		const userSignature = wx.getStorageSync('userSignature')
		if( userSignature !=='' ) {
			resolve(userSignature)
			return
		}
		const { code } = await wx.login()
   		const { openid } = await apiJscode2session({ code })
		wx.setStorageSync('userSignature', openid)
		resolve(openid)
	})
}



export async function getImageInfo(url) {
    const img = await wx.getImageInfo({
      src: url
    })
    return img 
  }

